home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World Komputer 2010 April
/
PCWorld0410.iso
/
pluginy Firefox
/
61266
/
61266.xpi
/
chrome
/
content
/
ff-overlay.js
< prev
next >
Wrap
Text File
|
2010-02-08
|
18KB
|
497 lines
if(!com) var com={};
if(!com.chechurin) com.chechurin={};
// Helper functions
com.chechurin.utils = {
// Set specific style to element or array of elements
// Usage:
// setStyle([div, someSpan], 'position', 'absolute')
setStyle: function(elements, prop, value){
for (var i = 0, len = elements.length-1; i <= len; ++i) {
elements[i].style[prop] = value;
}
},
// Add css styles to element or array elements
// Usage:
// css([foo, bar],{
// position: 'absolute',
// width: '100px'
// })
css: function(el, styles){
for (var prop in styles){
if (!styles.hasOwnProperty(prop)) continue;
com.chechurin.utils.setStyle(el, prop, styles[prop]);
}
}
};
com.chechurin.beststickynotes = function(){
var ECHE = com.chechurin.utils;
// Load sticky notes CSS to global stylessheets
var sss = Components.classes["@mozilla.org/content/style-sheet-service;1"]
.getService(Components.interfaces.nsIStyleSheetService);
var ios = Components.classes["@mozilla.org/network/io-service;1"]
.getService(Components.interfaces.nsIIOService);
var uri = ios.newURI("chrome://beststickynotes/skin/overlay.css", null, null);
if(!sss.sheetRegistered(uri, sss.USER_SHEET))
sss.loadAndRegisterSheet(uri, sss.USER_SHEET);
var BASE_Z_INDEX = 100000;
var STATE_DISPLAY = 0;
var STATE_EDIT = 1;
var STATE_MOVE = 2;
var STATE_RESIZE = 3;
// Min widh/height
var MIN_WIDTH = 200;
var MIN_HEIGHT;
var YpageXOffset, YpageYOffset;
// Sticky Notes class
var StickyNote = function(x, y, width, height, text, doc){
doc = doc || window.content.document;
var bod = doc.getElementsByTagName('body')[0];
var stickyNote = this;
stickyNote._moveX = 0;
stickyNote._moveY = 0;
stickyNote._state = STATE_DISPLAY;
stickyNote.container = doc.createElement("div");
stickyNote._x = x == undefined ? YpageXOffset : x;
stickyNote._y = y == undefined ? YpageYOffset : y;
stickyNote._width = width == undefined || width < MIN_WIDTH ? MIN_WIDTH: width;
stickyNote._height = height == undefined || height < MIN_HEIGHT ? MIN_HEIGHT : height;
ECHE.css([stickyNote.container],{
position: "absolute",
left: stickyNote._x + "px",
top: stickyNote._y + "px",
width: stickyNote._width + "px",
zIndex: BASE_Z_INDEX
});
stickyNote.container.className = "eche-best-sticky-note";
var div = doc.createElement("div");
div.className = "eche-best-sticky-note-top";
div.style.MozUserSelect="none";
var topTitle = doc.createElement("span");
topTitle.appendChild(doc.createTextNode("Xsticky-Tool"));
div.appendChild(topTitle);
// Remove Sticky note button
var a = doc.createElement("a");
// Remove Method
stickyNote.remove = function() {
stickyNote.container.parentNode.removeChild(stickyNote.container);
};
// Remove button click listener
a.addEventListener("click", function() {
if (window.confirm("Current Note will be completely removed. Continue?"))
{
stickyNote.remove();
removeStickyNote(stickyNote);
}
}, true);
var removeImg = doc.createElement("img");
removeImg.src = "chrome://beststickynotes/skin/xsticky/close1.jpg";
a.className = "eche-best-sticky-note-top-close";
a.title="Remove this sticky note";
a.appendChild(removeImg);
div.appendChild(a);
// Minimize Sticky note button
var min = doc.createElement("a");
// Minimize button click listener
min.addEventListener("click", function() {
stickyNote.displayText.style.display = stickyNote.displayText.style.display == "none" ? "block" : "none";
minImg.src = minImg.src == "chrome://beststickynotes/skin/xsticky/minimize1.jpg" ? "chrome://beststickynotes/skin/xsticky/maximize1.jpg" : "chrome://beststickynotes/skin/xsticky/minimize1.jpg";
}, true);
var minImg = doc.createElement("img");
minImg.src = "chrome://beststickynotes/skin/xsticky/minimize1.jpg";
min.className = "eche-best-sticky-note-top-close";
min.title="Minimize this sticky note";
min.appendChild(minImg);
div.appendChild(min);
// Settings Sticky note button
var settings = doc.createElement("a");
// Settings button click listener
settings.addEventListener("click", function() {
alert("Current Note properties still in development, wait for update.");
}, true);
var setImg = doc.createElement("img");
setImg.src = "chrome://beststickynotes/skin/xsticky/properties1.jpg";
settings.className = "eche-best-sticky-note-top-close";
settings.title="Current sticky note properties";
settings.appendChild(setImg);
div.appendChild(settings);
// Append Sticky body to container
stickyNote.container.appendChild(div);
var resizeGrabber = doc.createElement("div");
resizeGrabber.className = "eche-best-sticky-note-resize-grabber";
stickyNote.container.appendChild(resizeGrabber);
stickyNote.displayText = doc.createElement("div");
stickyNote.displayText.title = "double click to edit sticky note";
stickyNote.displayText.className = "eche-best-sticky-note-display-text";
stickyNote.editText = doc.createElement("textarea");
stickyNote.editText.className = "eche-best-sticky-note-edit-text";
ECHE.css([stickyNote.displayText,stickyNote.editText],{
fontFamily: prefs.getCharPref("font"),
fontSize: prefs.getIntPref("fontsize") + "pt",
lineHeight: prefs.getIntPref("fontsize")*2 + "px",
height: stickyNote._height + "px"
});
ECHE.css([stickyNote.editText],{
display: "none",
border: "none",
margin: "0px",
});
ECHE.css([stickyNote.displayText],{
opacity: prefs.getCharPref("transparency")
});
// Bottom div constructor
var divBottom = doc.createElement("div");
divBottom.className = "eche-best-sticky-note-bottom";
divBottom.style.MozUserSelect="none";
var home = doc.createElement("a");
home.href = "http://xsticky.com/";
// Home button click listener
home.addEventListener("click", function(event) {
event.stopPropagation();
event.preventDefault();
gBrowser.selectedTab = gBrowser.addTab("http://xsticky.com/");
}, true);
var homeImg = doc.createElement("img");
homeImg.src = "chrome://beststickynotes/skin/xsticky/home1.jpg";
home.appendChild(homeImg);
divBottom.appendChild(home);
stickyNote.container.appendChild(stickyNote.displayText);
stickyNote.container.appendChild(stickyNote.editText);
stickyNote.container.appendChild(divBottom);
// Resize grabber event listener
resizeGrabber.addEventListener("mousedown", function(e) {
if (e.button == 0 && stickyNote._state != STATE_EDIT) {
stickyNote.eventHelper(e, STATE_RESIZE);
return false;
}
return true;
}, true);
// Mouse up, down, move event listeners
div.addEventListener("mousedown", function(e) {
if (e.button == 0 && stickyNote._state == STATE_DISPLAY) {
stickyNote.eventHelper(e, STATE_MOVE);
div.style.cursor = "move";
return false;
}
return true;
}, true);
window.addEventListener("mouseup", function(e) {
if (e.button == 0 && (stickyNote._state == STATE_MOVE || stickyNote._state == STATE_RESIZE)) {
bod.style.MozUserSelect="text";
div.style.cursor = "default";
stickyNote._state = STATE_DISPLAY;
saveStickyNotes(stickyNote);
}
}, true);
window.addEventListener("mousemove", function(e) {
switch (stickyNote._state) {
case STATE_MOVE:
stickyNote._x += e.pageX - stickyNote._moveX;
stickyNote._y += e.pageY - stickyNote._moveY;
stickyNote.container.style.left = stickyNote._x + "px";
stickyNote.container.style.top = stickyNote._y + "px";
stickyNote._moveX = e.pageX;
stickyNote._moveY = e.pageY;
return false;
case STATE_RESIZE:
var newWidth = stickyNote._width + e.pageX - stickyNote._moveX;
var newHeight = stickyNote._height + e.pageY - stickyNote._moveY;
if (newWidth >= MIN_WIDTH) {
stickyNote._width = newWidth;
stickyNote._moveX = e.pageX;
stickyNote.container.style.width = stickyNote._width + "px";
}
if (newHeight >= MIN_HEIGHT) {
stickyNote._height = newHeight;
stickyNote._moveY = e.pageY;
stickyNote.editText.style.height = stickyNote.displayText.style.height = stickyNote._height + "px";
}
return false;
}
return true;
}, true);
// Dbclick on Sticky Note listener
stickyNote.container.addEventListener("dblclick", function() {
// stickyNote.displayText.style.opacity = "1";
stickyNote.displayText.style.display="none";
stickyNote.editText.style.display="block";
stickyNote._state = STATE_EDIT;
stickyNote.editText.focus();
}, true);
// End of editing Sticky Note
function endEdit() {
stickyNote.displayText.style.opacity = prefs.getCharPref("transparency");
stickyNote.displayText.style.display="block";
stickyNote.editText.style.display="none";
stickyNote._state = STATE_DISPLAY;
stickyNote.displayText.innerHTML = stickyNote.editText.value.replace(/\n/g,"<br/>");
saveStickyNotes(stickyNote);
};
stickyNote.editText.addEventListener("keyup", function(e) {
if (e.which == 27) {
endEdit();
}
}, true);
stickyNote.editText.addEventListener("blur", endEdit, true);
stickyNote.getX = function() {
return stickyNote._x;
}
stickyNote.getY = function() {
return stickyNote._y;
}
stickyNote.getWidth = function() {
return stickyNote._width;
}
stickyNote.getHeight = function() {
return stickyNote._height;
}
stickyNote.setX = function(x) {
stickyNote._x = x;
stickyNote.container.style.left = x + "px";
}
stickyNote.setY = function(y) {
stickyNote._y = y;
stickyNote.container.style.top = y + "px";
}
stickyNote.setWidth = function(width) {
stickyNote.container.style.width = width + "px";
stickyNote._width = width;
}
stickyNote.setHeight = function(height) {
stickyNote.editText.style.height = stickyNote.displayText.style.height = height + "px";
stickyNote._height = height;
}
// Setting Sticky Note text
stickyNote.setText = function(text) {
stickyNote.displayText.innerHTML = text.replace(/\n/g,"<br/>");
stickyNote.editText.value = text;
};
// Getting Sticky Note text
stickyNote.getText = function() {
return stickyNote.editText.value;
};
// Events helper function
stickyNote.eventHelper = function(e, STATE){
bod.style.MozUserSelect="none";
this._state = STATE;
this._moveX = e.pageX;
this._moveY = e.pageY;
}
if (text != undefined) {
stickyNote.setText(text);
}
stickyNote.displayText.style.MozUserSelect = "none";
resizeGrabber.style.MozUserSelect="none";
bod.appendChild(stickyNote.container);
};
function saveStickyNotes(stickyNote) {
if (stickyNotesColl.links.length != 0) {
var doc = window.content.document;
var uri = doc.location.href;
var bTitle = doc.title;
var pushToColl = true;
for (var i = 0; i < stickyNotesColl.links.length; i++ ) {
if ( stickyNotesColl.links[i] == uri ) {
stickyNotesColl.links[i] == uri;
stickyNotesColl.text[i]=stickyNote.getText();
stickyNotesColl.x[i]=stickyNote.getX();
stickyNotesColl.y[i]=stickyNote.getY();
stickyNotesColl.width[i]=stickyNote.getWidth();
stickyNotesColl.height[i]=stickyNote.getHeight();
pushToColl = false;
}
}
if(pushToColl){
stickyNotesColl.links.push(uri);
stickyNotesColl.text.push(stickyNote.getText());
stickyNotesColl.x.push(stickyNote.getX());
stickyNotesColl.y.push(stickyNote.getY());
stickyNotesColl.width.push(stickyNote.getWidth());
stickyNotesColl.height.push(stickyNote.getHeight());
pushToColl = false;
// Creating new bookmark for Sticky Note
var bmsvc = Components.classes["@mozilla.org/browser/nav-bookmarks-service;1"]
.getService(Components.interfaces.nsINavBookmarksService);
uri = ios.newURI(uri, null, null);
if (!bmsvc.isBookmarked(uri)) {
bmsvc.insertBookmark(prefs.getIntPref("folderId"), uri, bmsvc.DEFAULT_INDEX, bTitle);
}
}
}
savePreferences();
}
function removeStickyNote(stickyNote) {
var doc = window.content.document;
var uri = doc.location.href;
stickyNotesColl.links = [note for each (note in stickyNotesColl.links) if (note != uri)];
stickyNotesColl.x = [x for each (x in stickyNotesColl.x) if (x != stickyNote.getX())];
stickyNotesColl.y = [y for each (y in stickyNotesColl.y) if (y != stickyNote.getY())];
stickyNotesColl.width = [width for each (width in stickyNotesColl.width) if (width != stickyNote.getWidth())];
stickyNotesColl.height = [height for each (height in stickyNotesColl.height) if (height != stickyNote.getHeight())];
stickyNotesColl.text = [text for each (text in stickyNotesColl.text) if (text != stickyNote.getText())];
savePreferences();
}
var prefs = Components.classes["@mozilla.org/preferences-service;1"]
.getService(Components.interfaces.nsIPrefService);
prefs = prefs.getBranch("extensions.beststickynotes.");
function savePreferences(){
prefs.setCharPref("stickyNotes", JSON.stringify(stickyNotesColl));
}
var stickyNotes = '';
var stickyNotesColl;
var pub = {};
// On Fx load gets stickyNotes, parses JSON and added event listeners.
// Also checked if Bookmarks folder created, and if not - creates
pub.onFirefoxLoad = function(event) {
// checks existing of bookmarks folder, and creates:
var bmsvc = Components.classes["@mozilla.org/browser/nav-bookmarks-service;1"]
.getService(Components.interfaces.nsINavBookmarksService);
var menuFolder = bmsvc.bookmarksMenuFolder; // Bookmarks menu folder
var folderId = prefs.getIntPref("folderId");
MIN_HEIGHT = prefs.getIntPref("fontsize")*1.75;
try
{
bmsvc.getItemType(folderId);
}
catch(e)
{
var newFolderId = bmsvc.createFolder(menuFolder, "Sticky Notes", bmsvc.DEFAULT_INDEX);
prefs.setIntPref("folderId", newFolderId);
}
// Gets JSON sticky notes collection
stickyNotes = prefs.getCharPref("stickyNotes");
stickyNotesColl = JSON.parse(stickyNotes);
document.getElementById("contentAreaContextMenu")
.addEventListener("popupshowing", function (e){ pub.showFirefoxContextMenu(e); }, false);
var appcontent = document.getElementById("appcontent"); // browser
if(appcontent)
appcontent.addEventListener("DOMContentLoaded", pub.onPageLoad, true);
};
pub.onPageLoad = function(aEvent) {
var doc = aEvent.originalTarget; // doc is document that triggered "onload" event
for (i in stickyNotesColl.links){
if (stickyNotesColl.links[i]==doc.location.href)
{
new StickyNote(stickyNotesColl.x[i], stickyNotesColl.y[i], stickyNotesColl.width[i], stickyNotesColl.height[i], stickyNotesColl.text[i],doc);
}
}
};
pub.showFirefoxContextMenu = function(event) {
// show or hide the menuitem based on what the context menu is on
document.getElementById("context-beststickynotes").hidden = gContextMenu.onImage;
};
pub.addStickyNote = function(e){
// checked if clicked right mouse button
e = e || window.event;
if (e.target.id =="context-beststickynotes")
{
var newStickyNote = new StickyNote();
}
else if (e.target.id =="beststickynotes-statusbar")
{
var doc = doc || window.content.document;
var bod = doc.getElementsByTagName('body')[0];
var newStickyNote = new StickyNote((bod.clientWidth-MIN_WIDTH)/2, ((window.innerHeight)/3+doc.defaultView.scrollY));
}
};
pub.onClick = function(e) {
// checked if clicked right mouse button
e = e || window.event;
if (e.button == 2 && e.target.id !="beststickynotes-statusbar")
{
YpageXOffset = e.pageX;
YpageYOffset = e.pageY;
}
};
return pub;
}();
window.addEventListener("load", com.chechurin.beststickynotes.onFirefoxLoad, false);
window.addEventListener("click", com.chechurin.beststickynotes.onClick, false);